//
//  OrderDetailRootVC.m
//  Zhiche-Carriage
//
//  Created by LeeBruce on 16/6/14.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#define  order_pay  @"order/noreceivedetail"//待收款详情
#define order_done @"order/donedetail" //全部

#import "OrderDetailRootVC.h"
#import "TopBackView.h"
#import <Masonry.h>
#import "AddressCell.h"
#import "RootViewController.h"
#import "TimeCell.h"
#import "CarStyleCell.h"
#import "AdviceCell.h"
#import "PriceCell.h"
#import "OrderCollectionViewCell.h"
#import "Common.h"
#import <UIImageView+WebCache.h>

@interface OrderDetailRootVC ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate>

@property (nonatomic,strong) UILabel *statusLabel;

@property (nonatomic,strong) NSMutableArray *sectionArray;

@property (nonatomic,strong) NSMutableArray *testArray;
//@property (nonatomic,strong) UICollectionView *collectionView;



@end

@implementation OrderDetailRootVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.sectionArray = [NSMutableArray arrayWithObjects:@"地址",@"时间",@"车型信息",@"费用" ,nil];
    self.dataDictionary = [NSMutableDictionary dictionary];

    self.testArray = [NSMutableArray arrayWithObjects:@(2),@(1),@(3), nil];
    
    TopBackView *topBackView= [[TopBackView alloc]initViewWithFrame:CGRectMake(0, 0, screenWidth, 64) andTitle:@"运单详情"];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

    self.navigationController.interactivePopGestureRecognizer.enabled = YES;

    topBackView.rightButton.hidden = YES;
    [topBackView.leftButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:topBackView];
    
    self.view.backgroundColor = WhiteColor;
    
//    [self initDataSource];
    
    [self initSubViews];
}



//-(void)initDataSource
//{
//    NSString *string ;
//    if ([self.status isEqualToString:@"代收款"]) {
//        string = [NSString stringWithFormat:@"%@%@?id=%ld",Main_interface,order_pay,[self.orderId integerValue]];
//    } else {
//        string = [NSString stringWithFormat:@"%@%@?id=%ld",Main_interface,order_done,[self.orderId integerValue]];
//    }
//    
//    
//    [Common requestWithUrlString:string contentType:application_json finished:^(id responseObj) {
//        
//        
//        if ([responseObj[@"success"] boolValue]) {
//            self.dataDictionary = [NSMutableDictionary dictionaryWithDictionary:responseObj[@"data"]];
//        }
//        
//        
//        
//    } failed:^(NSString *errorMsg) {
//        NSLog(@"%@",errorMsg);
//    }];
//    
//}


-(void)initSubViews
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, screenWidth, screenHeight - 64)];
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(screenWidth, screenHeight );
    self.scrollView.backgroundColor = GrayColor;
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, cellHeight )];
    view.backgroundColor = WhiteColor;
    
    UILabel *orderL = [[UILabel alloc]initWithFrame:CGRectMake(18, 0, 50 * kWidth, cellHeight)];
    orderL.font = Font(12);
    orderL.textColor = BlackTitleColor;
    orderL.text = @"运单号:";
    [view addSubview:orderL];
    
    self.orderNumberL = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(orderL.frame), 0, 200, cellHeight)];
    self.orderNumberL.font = Font(12);
    self.orderNumberL.textColor = BlackTitleColor;
    self.orderNumberL.text = @"CO201605230001";
    [view addSubview:self.orderNumberL];
    
//    self.statusLabel = [[UILabel alloc]initWithFrame:CGRectMake(screenWidth - 94, 0, 80, cellHeight)];
//    self.statusLabel.font =Font(15);
//    self.statusLabel.textAlignment = NSTextAlignmentRight;
//    self.statusLabel.textColor = RedColor;
//    self.statusLabel.text = @"未提车";
//    [view addSubview:self.statusLabel];
    
    [self.scrollView addSubview:view];
    
    
    
    [self initMiddleView];
    
}

-(void)initMiddleView
{
    NSInteger inte = 0;
    for (int i = 0; i < self.testArray.count; i ++) {
        
        inte += [self.testArray[i] integerValue];
        
    }
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, cellHeight + 10, screenWidth, screenHeight * 1.3) style:UITableViewStyleGrouped];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = GrayColor;
    
    [self.scrollView addSubview:self.tableView];
    self.tableView.scrollEnabled = NO;
    
//    [self initCollectionView];
}

/*
-(void)initCollectionView
{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(70 *kWidth, 100 * kHeight);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.tableView.frame) + 10, screenWidth, 2 * 140 *kHeight) collectionViewLayout:layout];
    layout.minimumInteritemSpacing = 0;
    
    [self.scrollView addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = WhiteColor;
    
    [self.collectionView registerClass:[OrderCollectionViewCell class] forCellWithReuseIdentifier:@"carInfoCell"];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    
     [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];

    
}

*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.sectionArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 1 ||section == 0) {
        return 2;
    } else if(section == 2 ){
        
        NSArray *array;
        if ([self.dataDictionary.allKeys count] > 0) {
            
         array  = self.dataDictionary[@"vehicles"];
            return array.count;

        } else {
            return 3;
        }
    } else {
        
        return  (self.hiddenPriceTeger) ? 0 : 1;
//        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        NSString *cellString = @"addressCell";
        AddressCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
        if (!cell) {
            cell = [[AddressCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellString];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (indexPath.row == 0) {
            
            cell.imageV.image = [UIImage imageNamed:@"Address_Cell_fache"];
            
        } else {
            
        cell.imageV.image = [UIImage imageNamed:@"Address_Cell_shouche"];
            
        }
        
        NSLog(@"%@",self.dataDictionary);
        
        if ([self.dataDictionary.allKeys count] > 0) {
            
            if (indexPath.row == 0) {
                
                if ([self.dataDictionary[@"departProvinceName"] isEqual:self.dataDictionary[@"departCityName"]]) {
                    
                       cell.firstLabel.text = [NSString stringWithFormat:@"%@-%@",self.dataDictionary[@"departProvinceName"],self.dataDictionary[@"departCountyName"]];
                } else {
                    
                       cell.firstLabel.text = [NSString stringWithFormat:@"%@-%@-%@",self.dataDictionary[@"departProvinceName"],self.dataDictionary[@"departCityName"],self.dataDictionary[@"departCountyName"]];
                }
                
                cell.secondLabel.text = [NSString stringWithFormat:@"%@",self.dataDictionary[@"departAddr"]];
                
                cell.thirdLabel.text = [NSString stringWithFormat:@"%@  %@",self.dataDictionary[@"departContact"],self.dataDictionary[@"departPhone"]];
             
                
            } else {
                
                if ([self.dataDictionary[@"receiptProvinceName"] isEqual:self.dataDictionary[@"receiptCityName"]]) {
                    
                      cell.firstLabel.text = [NSString stringWithFormat:@"%@-%@",self.dataDictionary[@"receiptProvinceName"],self.dataDictionary[@"receiptCountyName"]];
                } else {
                    
                      cell.firstLabel.text = [NSString stringWithFormat:@"%@-%@-%@",self.dataDictionary[@"receiptProvinceName"],self.dataDictionary[@"receiptCityName"],self.dataDictionary[@"receiptCountyName"]];
                }
                
                cell.secondLabel.text = [NSString stringWithFormat:@"%@",self.dataDictionary[@"receiptAddr"]];
                
                cell.thirdLabel.text = [NSString stringWithFormat:@"%@  %@",self.dataDictionary[@"receiptContact"],self.dataDictionary[@"receiptPhone"]];
                
              

            }
            
        }
        
    
        return cell;
 
    } else if ( indexPath.section == 1)
    {
        
        NSString *cellString = @"timeCell";
        TimeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
        if (!cell) {
            cell = [[TimeCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellString];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        if (indexPath.row == 0) {
            
            cell.statusL.text = @"提车时间";
            cell.imageV.image = [UIImage imageNamed:@"Address_Cell_tiche"];
            
            
        } else {
            cell.statusL.text = @"送达时间";
            cell.imageV.image = [UIImage imageNamed:@"Address_Cell_songda"
                                 ];

        }

        
        if ([self.dataDictionary.allKeys count] > 0) {
            
            if (indexPath.row == 0) {
                
                cell.dateLabel.text = [NSString stringWithFormat:@"%@",self.dataDictionary[@"shipmentDate"]];
                
                
            } else {
                cell.dateLabel.text = [NSString stringWithFormat:@"%@",self.dataDictionary[@"arriveDate"]];
            }
            
    
        }
        
        return cell;

    } else if ( indexPath.section == 2) {
        
        NSString *cellString = @"carStyle";
        CarStyleCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
        if (!cell) {
            cell = [[CarStyleCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellString];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

//        cell.number = [_testArray[indexPath.row] integerValue];
        
        
        if ([self.dataDictionary.allKeys count] > 0) {
            
            
            NSArray *array = self.dataDictionary[@"vehicles"];
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",array[indexPath.row][@"brandLogo"]]];
            
            [cell.imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"car_style1"]];

            cell.nameLabel.text = [NSString stringWithFormat:@"%@-%@",array[indexPath.row][@"brandName"],array[indexPath.row][@"vehicleName"]];
            
            cell.numberLabel.text = [NSString stringWithFormat:@"%@",array[indexPath.row][@"vin"]];
            
        }
        
        return cell;
        
    } else

    {
//        //个人接单
//        if ([self.dataDictionary[@"bidderType"] integerValue] == 10 ) {
        
            
            NSString *cellString = @"priceCell";
            PriceCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
            if (!cell) {
                cell = [[PriceCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellString];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            if ([self.dataDictionary.allKeys count] > 0) {
                
               
                
                cell.shouldLabel.text = [NSString stringWithFormat:@"￥%.2f",[self.dataDictionary[@"cost"] floatValue] ];
            }
            
            return cell;
    }
    
//        } else {
//            
//            return nil;
//        }
        
        
    }


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 100 * kHeight;
    } else if(indexPath.section == 2)
    {
        
        return 60 * kHeight;
        
    } else
    {
        return cellHeight;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//    if (section == 3) {
//        return 0.1;
//    } else {
    
    return cellHeight;
//    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    if (section == 2) {
        return cellHeight + 10;
    } else {
        return 10;

    }
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = WhiteColor;
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(18, 0, 100, cellHeight)];
    label.text = self.sectionArray[section];
    label.font = Font(14);
    label.textColor = Color_RGB(14, 14, 14, 1);
    [view addSubview:label];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(screenWidth - 60, 0, 60, cellHeight)];
    label1.font = Font(15);
    [view addSubview:label1];
    
    if (section <3) {
        label1.hidden = YES;
    }
    

    
    if (section == 3) {
        
        if (self.hiddenPriceTeger) {
            return  nil;
        }else {
         
            return view;

        }
        
    } else {
    
        
        
        return view;
    }
   
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 2) {
        UIView *bigview = [[UIView alloc]init];
        bigview.backgroundColor = GrayColor;
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, cellHeight)];
        view.backgroundColor = WhiteColor;
        
        UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(screenWidth - 218, 0, 200, cellHeight)];
        label1.font = Font(13);
        label1.textColor = YellowColor;
        label1.textAlignment = NSTextAlignmentRight;
        label1.text = [NSString stringWithFormat:@"车辆总数  %@",self.dataDictionary[@"detail"]];
        [view addSubview:label1];
        [bigview addSubview:view];
        
        return bigview;
    } else {
        return nil;
    }
    
    
}

-(void)viewWillAppear:(BOOL)animated
{
    RootViewController *rootVC = [RootViewController defaultsTabBar];
    [rootVC setTabBarHidden:YES];
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark  UICollectionViewDelegate,UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return 2;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellIden = @"carInfoCell";
    OrderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIden forIndexPath:indexPath];
    
    cell.ImageView.backgroundColor = YellowColor;
    
    if (indexPath.section == 0) {

//        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",self.dataDictionary[@"loadPics"][indexPath.row][@"picKey"]]];
//        [cell.ImageView sd_setImageWithURL:url];
        
    }
    
    if (indexPath.section == 1) {
       
//        
//        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",self.dataDictionary[@"unloadPics"][indexPath.row][@"picKey"]]];
//        [cell.ImageView sd_setImageWithURL:url];
    }
    
    
    return cell;
    
}
//头标题尺寸
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(screenWidth, cellHeight);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(screenWidth, 10);
}

//items的偏移量
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 14, 5, 14);
}

//头标题
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
  
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader)
    {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        
        
        for (UILabel *view in headerView.subviews) {
            [view removeFromSuperview];
        }
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(14, 0, 200, headerView.frame.size.height)];
        lable.font = Font(13);
        lable.textColor = Color_RGB(14, 14, 14, 1);
        
        NSArray *array = @[@"提车信息",@"交车信息"];
        
        lable.text = array[indexPath.section];
        
        [headerView addSubview:lable];
        headerView.backgroundColor = WhiteColor;
        UILabel *lineL = [[UILabel alloc]initWithFrame:CGRectMake(0, cellHeight - 0.5, screenWidth, 0.5)];
        lineL.backgroundColor = LineGrayColor;
        [headerView addSubview:lineL];
        
        
        reusableview = headerView;
    }
    
        if (kind == UICollectionElementKindSectionFooter)
        {
            UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];
    
            reusableview = footerview;
            footerview.backgroundColor = GrayColor;
        }
    
    
    return reusableview;
    
    
}

*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
